const { DataTypes } = require("Sequelize");
const sequelize = require("./index");

const BookModule = sequelize.define(
  "dzl_BookModule",
  {
    book_id: {
      type: DataTypes.CHAR,
      allowNull: false,
      primaryKey: true,
    },
    book_name: {
      type: DataTypes.CHAR,
      allowNull: false,
      // unique: true,
    },
    author: {
      type: DataTypes.CHAR,
      allowNull: false,
    },
    publisher: {
      type: DataTypes.CHAR,
      allowNull: false,
    },
    price: {
      type: DataTypes.INTEGER,
      allowNull: false,
      validate: {
        min: 0,
      },
    },
    number: {
      type: DataTypes.INTEGER,
      allowNull: false,
      validate: {
        min: 0,
      },
    },
    place: {
      type: DataTypes.CHAR,
      allowNull: true,
    },
    bookImgUrl: {
      type: DataTypes.CHAR,
      allowNull: true,
    },
    desc: {
      type: DataTypes.CHAR,
      allowNull: false,
      defaultValue: "这是书籍的默认描述",
    },
    tag: {
      type: DataTypes.CHAR,
      allowNull: false,
      defaultValue: "其他",
      get() {
        const rawValue = this.getDataValue("tag");
        return rawValue ? rawValue.split("-") : null;
      },
    },
  },
  {
    tableName: "books",
  }
);

//
const promise = BookModule.sync({ alter: true });
promise.then(
  (success) => {
    console.log("BookModule模型同步成功", success);
  },
  (fail) => {
    console.log("BookModule模型同步失败", fail);
  }
);

module.exports = BookModule;
